package com.ja.openchat.viewmodels;

import android.app.Application;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.ja.openchat.ui.NameAuthenFragment;
import com.ja.openchat.ui.NameAuthenNumberFragment;
import com.ja.openchat.ui.me.PaymentReceivedCodeFragment;
import com.ja.openchat.ui.me.RechargeFragment;
import com.ja.openchat.ui.me.RedPacketRecordViewPagerGroupFragment;
import com.ja.openchat.ui.me.SetPayPwdFragment;
import com.ja.openchat.ui.me.SetPayPwdOldFragment;
import com.ja.openchat.ui.me.TransactionRecordFragment;
import com.ja.openchat.ui.me.WithdrawalAccountFragment;
import com.ja.openchat.ui.me.WithdrawalFragment;
import com.liaobeicms.http.api.ApiServer;
import com.liaobeicms.http.bean.BaseResponse;
import com.liaobeicms.http.bean.NameAuthenBean;
import com.liaobeicms.http.net.RetrofitManager;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.user.UserInfo;
import com.zy.openchat.core.model.wallet.WalletInfo;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;


public class WalletViewModel extends BaseViewModel {
    public ObservableField<String> walletValue = new ObservableField<>("0.00");
    private boolean hasPaymentPassword = true;
    public UIChangeLiveData mUC = new UIChangeLiveData();

    public WalletViewModel(@NonNull Application application) {
        super(application);
    }

    public final class UIChangeLiveData extends SingleLiveEvent {
        private SingleLiveEvent<Boolean> showPaymentPasswordDialogEvent;

        public SingleLiveEvent<Boolean> getShowPaymentPasswordDialogEvent() {
            return showPaymentPasswordDialogEvent = new SingleLiveEvent<>();
        }
    }

    //提交按钮的点击事件
    public BindingCommand confirmOnClickCommand = new BindingCommand(() -> ToastUtils.showShort("点击事件"));
    //提现点击事件
    public BindingCommand withdrawalOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(WithdrawalFragment.class.getCanonicalName());
    });
    //充值点击事件
    public BindingCommand rechargeOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(RechargeFragment.class.getCanonicalName());
    });
    //实名认证点击事件
    public BindingCommand nameAuthenOnClickCommand = new BindingCommand(() -> {
        postStatusData();
    });
    //收款码点击事件

    public BindingCommand receivedCodeOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(PaymentReceivedCodeFragment.class.getCanonicalName());
    });
    //交易记录点击事件
    public BindingCommand transactionRecordOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(TransactionRecordFragment.class.getCanonicalName());
    });
    //红包记录点击事件
    public BindingCommand redPacketOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(RedPacketRecordViewPagerGroupFragment.class.getCanonicalName());
    });
    //支付密码点击事件
    public BindingCommand payPwdOnClickCommand = new BindingCommand(() -> {
        if (hasPaymentPassword) {
            startContainerActivity(SetPayPwdOldFragment.class.getCanonicalName());
        } else {
            startContainerActivity(SetPayPwdFragment.class.getCanonicalName());
        }
    });
    //提现账户点击事件
    public BindingCommand withdrawalAccountOnClickCommand = new BindingCommand(() -> {
        startContainerActivity(WithdrawalAccountFragment.class.getCanonicalName());
    });

    public void requestData() {
        TelegramManager.getTelegramManager().queryWalletInfo(getApplication(), new TgResultBlock<WalletInfo>() {
            @Override
            public void onResult(Map<String, Object> request, JsonObject response, WalletInfo walletInfo) {
                if (walletInfo != null) {
                    hasPaymentPassword = walletInfo.hasPaymentPassword;
                    if (!walletInfo.hasPaymentPassword) {
                        mUC.showPaymentPasswordDialogEvent.setValue(hasPaymentPassword);
                    }
                    walletValue.set(String.format("%.2f", walletInfo.balance));
                } else {
                    ToastUtils.showShort("请求超时，请稍后重试");
                }
            }

            @Override
            public void onTimeout(Map<String, Object> request) {
                ToastUtils.showShort("请求超时，请稍后重试");
            }
        });
    }

    private void postStatusData() {
        showProgress();
        String uid = String.valueOf(UserInfo.getUserInfo().id);
        toSubscribe(RetrofitManager.getInstance().create(ApiServer.class).postStatusData(uid), new DisposableObserver<BaseResponse<NameAuthenBean>>() {
            @Override
            public void onNext(BaseResponse<NameAuthenBean> jsonElementBaseResponse) {
                hideProgress();
                if (jsonElementBaseResponse.getCode() == 200) {
                    if ("-1".equals(jsonElementBaseResponse.getData().status)) {
                        startContainerActivity(NameAuthenFragment.class.getCanonicalName());
                    } else {
                        Bundle bundle = new Bundle();
                        bundle.putString("nameAuthenBean", new Gson().toJson(jsonElementBaseResponse.getData().info));
                        startContainerActivity(NameAuthenNumberFragment.class.getCanonicalName(), bundle);
                    }
                } else {
                    if (!TextUtils.isEmpty(jsonElementBaseResponse.getMsg())) {
                        ToastUtils.showShort(jsonElementBaseResponse.getMsg());
                    } else {
                        showErrorTip(getApplication().getString(R.string.request_error));
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                hideProgress();
                showErrorTip(getApplication().getString(R.string.request_error));
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
